package com.weilive.platform.gate.json.handler;

import org.springframework.stereotype.Service;

import com.weilive.core.netty.NettyErrors;
import com.weilive.core.protocol.json.JsonRequest;
import com.weilive.core.protocol.json.JsonRequestHandler;
import com.weilive.core.protocol.json.JsonResponse;
import com.weilive.core.utils.StringUtils;
import com.weilive.platform.PlatformErrors;
import com.weilive.platform.cache.app.AppManager;
import com.weilive.platform.cache.app.AppObj;
import com.weilive.platform.cache.app.AppObj.AppClientObj;
import com.weilive.platform.cache.app.AppObj.AppClusterObj;
import com.weilive.platform.gate.json.ObjectParser;
import com.weilive.platform.gate.json.protocol.CSA_PlatAppRoute;
import com.weilive.platform.gate.json.protocol.CSQ_PlatAppRoute;
import com.weilive.platform.gate.json.protocol.Master;

@Service
public class CSQ_PlatAppRoute_JsonHandler extends JsonRequestHandler {

	public Class<?> getRequestType() {
		return CSQ_PlatAppRoute.class;
	}

	public Class<?> getResponseType() {
		return CSA_PlatAppRoute.class;
	}

	public JsonResponse handleRequest(JsonRequest request) throws Exception {
		CSQ_PlatAppRoute params = (CSQ_PlatAppRoute) request.getData();
		CSA_PlatAppRoute response = new CSA_PlatAppRoute();

		if (params.app_id == null || params.client_id == null || params.version == null) {
			return createErrorResponse(NettyErrors.BAD_REQUEST);
		}

		AppObj app = AppManager.getApp(params.app_id);
		if (app == null) {
			return createErrorResponse(PlatformErrors.APP_NOT_EXIST);
		}
		AppClientObj client = app.getClient(params.client_id);
		if (client == null) {
			return createErrorResponse(PlatformErrors.CLIENT_NOT_EXIST);
		}
		response.websit = app.info.website;
		AppClusterObj cluster = null;
		if (params.version.equals(client.info.dev_version)) {
			cluster = client.dev_cluster;
		} else {
			cluster = client.cluster;
			if (params.version.equals(client.info.version) == false) {

				response.latest_version = client.info.version;
				response.latest_upgrade = client.info.upgrade;
				response.download_url = client.info.download_url;
				response.need_upgrade = false;

				int[] version = StringUtils.parseVersion(params.version);
				int[] min_version = StringUtils.parseVersion(client.info.min_version);
				for (int i = 0; i < version.length; i++) {
					if (version[i] > min_version[i]) {
						break;
					} else if (version[i] < min_version[i]) {
						response.need_upgrade = true;
					}
				}
			}
		}
		if (cluster != null) {
			Master master = new Master();
			master.master_url = cluster.info.master_url;
			master.japi_url = cluster.info.japi_url;
			master.pay_url = cluster.info.pay_url;
			master.xmpp_url = cluster.info.xmpp_url;
			master.app_notice = ObjectParser.toJsonObj(cluster.geAppNotice());

			response.cluster_status = cluster.info.status;
			if (cluster.info.status != 0) {
				response.cluster_note = cluster.info.fix_note;
			}
			response.master = master;
		}

		return JsonResponse.create(response);
	}

}
